﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Mozlite.Components;

namespace Mozlite
{
    /// <summary>
    /// 全局应用程序上下文。
    /// </summary>
    public sealed class AppContext
    {
        #region initializer
        private AppContext() { }
        [ThreadStatic]
        private static AppContext appContext;
        private const string CONTEXTKEY = "mozcms[AppContext]";

        /// <summary>
        /// 获取当前应用程序上下文。
        /// </summary>
        public static AppContext Current
        {
            get
            {
                var httpContext = HttpContext.Current;
                if (httpContext != null)
                {
                    var current = httpContext.Items[CONTEXTKEY] as AppContext;
                    if (current == null)
                    {
                        current = new AppContext();
                        httpContext.Items[CONTEXTKEY] = current;
                    }
                    return current;
                }
                else if (appContext == null)
                    appContext = new AppContext();
                return appContext;
            }
        }

        private HttpContextBase httpContext;
        /// <summary>
        /// 获取当前请求上下文实例对象。
        /// </summary>
        public HttpContextBase Context
        {
            get
            {
                if(httpContext == null && HttpContext.Current != null)
                    httpContext = new HttpContextWrapper(HttpContext.Current);
                return httpContext;
            }
        }
        #endregion

        #region culture
        private string cultureName;
        /// <summary>
        /// 区域名称。
        /// </summary>
        public string CultureName {
            get {
                if (cultureName == null)
                    cultureName = Cores.CurrentCulture.Name;
                return cultureName;
            }
        }
        #endregion

        #region theme
        private string theme;
        /// <summary>
        /// 获取或设置当前请求模板。
        /// </summary>
        public string Theme
        {
            get
            {
                if (theme == null)
                    theme = GlobalSettings.Current.DefaultTheme ?? "default";
                return theme;
            }
            set {
                theme = value;
            }
        }
        #endregion

        #region security
        /// <summary>
        /// 获取当前用户名称。
        /// </summary>
        public string UserName {
            get {
                return User.UserName;
            }
        }
        /// <summary>
        /// 获取当前用户ID。
        /// </summary>
        public int UserID
        {
            get
            {
                return User.UserID;
            }
        }
        private User user;
        /// <summary>
        /// 获取或设置当前用户。
        /// </summary>
        public User User {
            get {
                if(user == null)
                    user = Users.GetUser(Context.User.Identity.Name) ??
                        new User
                        {
                            UserName = Users.Anonymous,
                            PublicKey = Users.GetAnonymousID()
                        };
                return user;
            }
            set { user = value; }
        }
        #endregion

        #region pageindex pagesize
        private int pageIndex = -1;
        /// <summary>
        /// 页码，从1开始。
        /// </summary>
        public int PageIndex
        {
            get
            {
                if(pageIndex == -1)
                {
                    var page = Context.Request["Page"];
                        pageIndex = page.To<int>(1);
                    if(pageIndex < 1)
                        pageIndex = 1;
                }
                return pageIndex;
            }
        }

        private int pageSize = 0;
        /// <summary>
        /// 每页显示的记录数。
        /// </summary>
        public int PageSize
        {
            get
            {
                if(pageSize == 0)
                {
                    var size = Context.Request["PageSize"];
                    pageSize = size.To<int>(Consts.DefaultPageSize);
                }
                return pageSize;
            }
            set
            {
                pageSize = value;
            }
        }
        #endregion
    }
}
